package com.dy.project.service.impl;

import com.dy.project.domain.*;
import com.dy.project.exception.CustomException;
import com.dy.project.mapper.McCommoditySignMapper;
import com.dy.project.service.IMcCommodityService;
import com.dy.project.service.IMcCommoditySignService;
import com.dy.project.utils.OnlineUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 商品签收Service业务层处理
 *
 * @author ruoyi
 * @date 2020-07-29
 */
@Service
public class McCommoditySignServiceImpl implements IMcCommoditySignService {
    private static final int OPTION_SUPPLY = 1;
    private static final int OPTION_RESTORE_SUPPLY = 2;
    private static final int OPTION_DELETE = 3;
    private static final int OPTION_RESTORE_DELETE = 4;
    @Autowired
    private McCommoditySignMapper mcCommoditySignMapper;
    @Autowired
    private IMcCommodityService commodityService;
    @Autowired
    private OnlineUserUtil onlineUserUtil;


    /**
     * 查询商品签收
     *
     * @param csId 商品签收ID
     * @return 商品签收
     */
    @Override
    public McCommoditySign selectMcCommoditySignById(Long csId) {
        return mcCommoditySignMapper.selectMcCommoditySignById(csId);
    }

    @Override
    public List<CommoditySignGroupTemplate> selectMcCommoditySignListGroup(CommoditySignGroupTemplate commoditySignGroupTemplate) {
        return mcCommoditySignMapper.selectMcCommoditySignListGroup(commoditySignGroupTemplate);
    }

    @Override
    public List<CommoditySignGroupTemplate> selectCommoditySignListGroupByAcId(CommoditySignGroupTemplate commoditySignGroupTemplate) {
        if (commoditySignGroupTemplate.getAcId() == null) {
            throw new CustomException("未獲取到商品編號");
        }
        return mcCommoditySignMapper.selectCommoditySignListGroupByAcId(commoditySignGroupTemplate);
    }

    @Override
    public List<CommoditySignTemplate> listCommoditySignTemplate(CommoditySignTemplate commoditySignTemplate) {
        return mcCommoditySignMapper.listCommoditySignTemplate(commoditySignTemplate);
    }

    @Override
    public int insertMcCommoditySign(McCommoditySign mcCommoditySign) {
        if (mcCommoditySign.getCsOrderCount() < 1) {
            throw new CustomException("下單數量不可小於1");
        }
        return mcCommoditySignMapper.insertMcCommoditySign(mcCommoditySign);
    }

    @Override
    public int updateSignDtlState(HashMap<String, Object> params) {
        if (params == null) {
            throw new CustomException("未獲取到參數");
        }
        Long csId;
        int option;
        try {
            csId = ((Integer) params.get("csId")).longValue();
            option = (int) params.get("option");
        } catch (Exception exception) {
            exception.printStackTrace();
            throw new CustomException("轉換參數時發生錯誤");
        }
        McCommoditySign commoditySign = this.selectMcCommoditySignById(csId);
        if (commoditySign == null) {
            throw new CustomException("未獲取到編號為" + csId + "的簽收子記錄");
        }
        if (commoditySign.getCsState() == McCommoditySign.CS_STATE_CONFIRM) {
            throw new CustomException("該記錄已被簽收");
        }
        int supplyState = commoditySign.getCsSupplyState();
        int deleteState = commoditySign.getCsDeleteState();
        McCommoditySign updateSign = new McCommoditySign();
        switch (option) {
            case OPTION_SUPPLY:
                if (supplyState == McCommoditySign.CS_SUPPLY) {
                    throw new CustomException("該記錄已補貨");
                } else if (deleteState == McCommoditySign.CS_DELETE) {
                    throw new CustomException("該記錄已刪除");
                }
                updateSign.setCsSupplyState(McCommoditySign.CS_SUPPLY);
                break;
            case OPTION_RESTORE_SUPPLY:
                if (supplyState == McCommoditySign.CS_UNSUPPLY) {
                    throw new CustomException("該記錄未補貨");
                } else if (deleteState == McCommoditySign.CS_DELETE) {
                    throw new CustomException("該記錄已刪除");
                }
                updateSign.setCsSupplyState(McCommoditySign.CS_UNSUPPLY);
                break;
            case OPTION_DELETE:
                if (supplyState == McCommoditySign.CS_SUPPLY) {
                    throw new CustomException("該記錄已補貨");
                } else if (deleteState == McCommoditySign.CS_DELETE) {
                    throw new CustomException("該記錄已刪除");
                }
                updateSign.setCsDeleteState(McCommoditySign.CS_DELETE);
                break;
            case OPTION_RESTORE_DELETE:
                if (supplyState == McCommoditySign.CS_SUPPLY) {
                    throw new CustomException("該記錄已補貨");
                } else if (deleteState == McCommoditySign.CS_UNDELETE) {
                    throw new CustomException("該記錄未刪除");
                }
                updateSign.setCsDeleteState(McCommoditySign.CS_UNDELETE);
                break;
        }
        updateSign.setCsId(csId);
        updateSign.setUpdateTime(new Date());
        return mcCommoditySignMapper.updateSignDtlState(updateSign);
    }

    @Override
    @Transactional
    public int handleSignCommodityBatch(CommoditySignBatchOperationTemplate commoditySignBatchOperationTemplate) {
        Long acId = commoditySignBatchOperationTemplate.getAcId();
        if (acId == null) {
            throw new CustomException("未獲取到商品編號");
        }
        McCommodity commodity = commodityService.selectMcCommodityById(acId);
        if (commodity == null) {
            throw new CustomException("未獲取到編號為" + acId + "的商品信息");
        }
        List<String> dtStaffNos = commoditySignBatchOperationTemplate.getDtStaffNos();
        if (dtStaffNos == null || dtStaffNos.size() == 0) {
            throw new CustomException("未獲取到囚犯編號組");
        }
        boolean isSupply = commoditySignBatchOperationTemplate.isSupply();
        boolean isDelete = commoditySignBatchOperationTemplate.isDelete();
        if (!isSupply && !isDelete) {
            throw new CustomException("未獲取到操作類型");
        }
        if (isSupply && isDelete) {
            throw new CustomException("非法的操作類型");
        }
        return mcCommoditySignMapper.handleSignCommodityBatch(commoditySignBatchOperationTemplate);
    }

    @Override
    @Transactional
    public int sign(McCommoditySign mcCommoditySign) {
        if (mcCommoditySign.getCsId() == null) {
            throw new CustomException("未獲取到簽收記錄編號");
        }
        McCommoditySign mcCommoditySignFlag = mcCommoditySignMapper.selectMcCommoditySignById(mcCommoditySign.getCsId());
        if (mcCommoditySignFlag == null) {
            throw new CustomException("未獲取到編號為" + mcCommoditySign.getCsId() + "的簽收記錄");
        }
        long lessCount = mcCommoditySignFlag.getCsSignCount() - mcCommoditySign.getCsSignCount();
        if (lessCount < 0) {
            throw new CustomException("簽收數量不可高於交易確認數量");
        }
        mcCommoditySign.setCsState(McCommoditySign.CS_STATE_CONFIRM);
        mcCommoditySign.setCsCheckPoliceNo(onlineUserUtil.getLoginUser().getCouPoliceNo());
        int updateResult = mcCommoditySignMapper.updateMcCommoditySign(mcCommoditySign);
//        if (lessCount > 0) {
//            mcCommoditySign.setCsSupplyState(McCommoditySign.CS_SUPPLY);
//            mcCommoditySign.setCsState(McCommoditySign.CS_STATE_UNCONFIRM);
//            mcCommoditySign.setCsOrderCount(lessCount);
//            mcCommoditySign.setCsSignCount(lessCount);
//            return insertMcCommoditySign(mcCommoditySign);
//        }
        return updateResult;
    }

    @Override
    @Transactional
    public int signAll(List<McCommoditySign> signList) {
        if(signList == null || signList.size() == 0){
            throw new CustomException("未獲取到簽收項");
        }
//        List<McCommoditySign> addList = new ArrayList<>();
        for (McCommoditySign mcCommoditySign : signList) {
            if (mcCommoditySign.getCsId() == null) {
                throw new CustomException("未獲取到簽收記錄編號");
            }
            McCommoditySign mcCommoditySignFlag = mcCommoditySignMapper.selectMcCommoditySignById(mcCommoditySign.getCsId());
            if (mcCommoditySignFlag == null) {
                throw new CustomException("未獲取到編號為" + mcCommoditySign.getCsId() + "的簽收記錄");
            }
            long lessCount = mcCommoditySignFlag.getCsSignCount() - mcCommoditySign.getCsSignCount();
            if (lessCount < 0) {
                throw new CustomException("簽收數量不可高於交易確認數量");
            }
            mcCommoditySign.setCsState(McCommoditySign.CS_STATE_CONFIRM);
            mcCommoditySign.setCsCheckPoliceNo(onlineUserUtil.getLoginUser().getCouPoliceNo());
            // int updateResult = mcCommoditySignMapper.updateMcCommoditySign(mcCommoditySign);
//            if (lessCount > 0) {
//                McCommoditySign mcCommoditySignClone;
//                try {
//                    mcCommoditySignClone = (McCommoditySign)mcCommoditySign.clone();
//                } catch (CloneNotSupportedException e) {
//                    e.printStackTrace();
//                    throw new CustomException("生成補貨單時發生異常");
//                }
//                mcCommoditySignClone.setCsSupplyState(McCommoditySign.CS_SUPPLY);
//                mcCommoditySignClone.setCsState(McCommoditySign.CS_STATE_UNCONFIRM);
//                mcCommoditySignClone.setCsOrderCount(lessCount);
//                mcCommoditySignClone.setCsSignCount(lessCount);
//                addList.add(mcCommoditySignClone);
//            }
        }
        int updateResult = mcCommoditySignMapper.signAll(signList);
//        if(addList.size() > 0){
//            return mcCommoditySignMapper.insertBatch(addList);
//        }
        return updateResult;
    }

    @Override
    public int updateSignCount(McCommoditySign commoditySign) {
        McCommoditySign updateObj = new McCommoditySign();
        updateObj.setCsId(commoditySign.getCsId());
        updateObj.setCsSignCount(commoditySign.getCsSignCount());
        return mcCommoditySignMapper.updateMcCommoditySign(updateObj);
    }
}
